<?php

namespace app\index\controller;

use think\Db;
use think\wxpay\example\JsApiPay;
use app\common\controller\Wechat;
use think\wxpay\lib\WxPayUnifiedOrder;
use think\wxpay\lib\WxPayConfig;
use think\wxpay\lib\WxPayApi;
use app\common\api\controller\Bonus;
use think\Validate;

class Test
{
    public function index()
    {
        $starttime = 1688041766;
        $endtime = 1688041931;
//        $sql1 = 'SELECT user_id,SUM(`value`) as points FROM yoshop_user_points_log WHERE create_time >= ".$starttime." AND create_time <=".$endtime." GROUP BY user_id';
//        $sql2 = 'SELECT user_id,SUM(money) AS credit4 FROM yoshop_user_credit4_log WHERE create_time >=".$starttime." AND create_time <=".$endtime." GROUP BY user_id';
//        $sql3 = 'SELECT user_id,SUM(money) AS yongjin FROM yoshop_user_yongjin_log WHERE create_time >=".$starttime." AND create_time <=".$endtime." GROUP BY user_id';
//
//        $arr1 = Db::query($sql1);
//        $arr2 = Db::query($sql2);
//        $arr3 = Db::query($sql3);
//        var_dump($arr1);die;

//        $user_id = Db::query("SELECT a.user_id,a.points as today_points,b.credit4 today_credit4,c.yongjin as today_yongjin FROM (SELECT user_id,SUM(`value`) as points FROM yoshop_user_points_log WHERE create_time >= ".$starttime." AND create_time <=".$endtime." GROUP BY user_id ) a
//LEFT JOIN (SELECT user_id,SUM(money) AS credit4 FROM yoshop_user_credit4_log WHERE create_time >=".$starttime." AND create_time <=".$endtime." GROUP BY user_id ) b ON a.user_id = b.user_id
//LEFT JOIN (SELECT user_id,SUM(money) AS yongjin FROM yoshop_user_yongjin_log WHERE create_time >=".$starttime." AND create_time <=".$endtime." GROUP BY user_id ) c ON a.user_id = c.user_id");
        $user_id = Db::query("select user_id from ceshi");
        var_dump($user_id);die;
        $u_id=Db::query("SELECT a.user_id,a.points as total_points,b.credit4 total_credit4,c.yongjin as total_yongjin FROM (SELECT user_id,SUM(`value`) as points FROM yoshop_user_points_log WHERE create_time <" .$starttime." and value<0 GROUP BY user_id ) a
LEFT JOIN (SELECT user_id,SUM(money) AS credit4 FROM yoshop_user_credit4_log WHERE create_time <".$starttime." and money>0 GROUP BY user_id ) b ON a.user_id = b.user_id
LEFT JOIN (SELECT user_id,SUM(money) AS yongjin FROM yoshop_user_yongjin_log WHERE create_time <".$starttime." and money>0 GROUP BY user_id ) c ON a.user_id = c.user_id");
        $arr_userid=array_column($u_id,NULL,'user_id');
        $arr = [];
        $arr_user=[];
        foreach ($user_id as $key => $value) {
            $value['create_time'] =strtotime(date('Y-m',time()));
            $value['total_points']= (isset($arr_userid[ $value['user_id'] ]) && !empty($arr_userid[ $value['user_id'] ]))? $arr_userid[ $value['user_id'] ]['total_points']:0;
            $value['total_credit4']=(isset($arr_userid[ $value['user_id'] ]) && (!empty($arr_userid[ $value['user_id'] ])))? $arr_userid[$value['user_id']]['total_credit4']:0;
            $value['total_yongjin']=(isset($arr_userid[ $value['user_id'] ]) && (!empty($arr_userid[ $value['user_id'] ])))? $arr_userid[$value['user_id']]['total_yongjin']:0;
            $arr[] = $value;
//$arr_user[]=$value['user_id'];

        }

        Db::transaction(function () use ($arr) {
            Db::name('user_day_log')->insertAll($arr,true);
        });
    }
}